﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using IntrepidGameLibrary;
using IntrepidGameLibrary.Controls;

namespace IntrepidGame.GameScreens
{
    public class MainMenuScreen : BaseGameState
    {

        #region Variables Region

        PictureBox backgroundImage;
        PictureBox arrowImageLeft;
        PictureBox arrowImageRight;
        LinkLabel startGame;
        LinkLabel loadGame;
        LinkLabel options;
        LinkLabel exitGame;

        float maxItemWidth = 0f;

        #endregion

        #region Constructor Region

        public MainMenuScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Functions

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager Content = GameRef.Content;

            /*backgroundImage = new PictureBox(
                Content.Load<Texture2D>(@"Backgrounds\TitleScreen" + GameRef.screenRectangle.Height),
                GameRef.screenRectangle);
            ControlManager.Add(backgroundImage);
            */

            Texture2D arrowTexture = Content.Load<Texture2D>(@"GUI\leftArrowUP");

            arrowImageLeft = new PictureBox(arrowTexture, new Rectangle(0, 0,
                arrowTexture.Width, arrowTexture.Height));
            ControlManager.Add(arrowImageLeft);

            arrowTexture = Content.Load<Texture2D>(@"GUI\rightArrowUP");

            arrowImageRight = new PictureBox(arrowTexture, new Rectangle(0, 0,
                arrowTexture.Width, arrowTexture.Height));
            ControlManager.Add(arrowImageRight);

            startGame = new LinkLabel();
            startGame.Text = "Start a New Game";
            startGame.Size = startGame.SpriteFont.MeasureString(startGame.Text);
            startGame.Selected += new EventHandler(menuItem_Selected);
            ControlManager.Add(startGame);


            loadGame = new LinkLabel();
            loadGame.Text = "Continue Game";
            loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);
            loadGame.Selected += menuItem_Selected;
            ControlManager.Add(loadGame);

            options = new LinkLabel();
            options.Text = "Options";
            options.Size = options.SpriteFont.MeasureString(options.Text);
            options.Selected += menuItem_Selected;
            ControlManager.Add(options);

            exitGame = new LinkLabel();
            exitGame.Text = "Exit to Windows";
            exitGame.Size = exitGame.SpriteFont.MeasureString(exitGame.Text);
            exitGame.Selected += menuItem_Selected;
            ControlManager.Add(exitGame);

            ControlManager.NextControl();

            ControlManager.FocusChanged += new EventHandler(ControlManager_FocusChanged);
            Vector2 position = new Vector2(GameRef.screenRectangle.Width / 2, GameRef.screenRectangle.Height - 300);

            foreach (Control c in ControlManager)
            {
                if (c is LinkLabel)
                {

                    if (c.Size.X > maxItemWidth)
                    {
                        maxItemWidth = c.Size.X;
                    }

                    c.Position = new Vector2(position.X - c.Size.X/2, position.Y);
                    position.Y += c.Size.Y + 5f;
                }
            }

            ControlManager_FocusChanged(startGame, null);

        }

        public override void Update(GameTime gameTime)
        {

            if (InputHandler.KeyReleased(Keys.Escape))
            {
                StateManager.PopState();
            }

            ControlManager.Update(gameTime, playerIndexInControl);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.spriteBatch.Begin();

            base.Draw(gameTime);

            ControlManager.Draw(GameRef.spriteBatch);

            GameRef.spriteBatch.End();
        }

        #endregion

        #region My Functions

        void ControlManager_FocusChanged(object sender, EventArgs e)
        {
            Control control = sender as Control;
            Vector2 position = new Vector2(GameRef.screenRectangle.Width/2 + maxItemWidth/2 + 10f,
                control.Position.Y);

            arrowImageRight.SetPosition(new Vector2(GameRef.screenRectangle.Width/2 - maxItemWidth/2 - arrowImageRight.Image.Width - 10f,  
                control.Position.Y));
            arrowImageLeft.SetPosition(position);
        }

        private void menuItem_Selected(object sender, EventArgs e)
        {
            if (sender == startGame)
            {
                StateManager.ChangeState(GameRef.battleScreen);
            }
            if (sender == loadGame)
            {
                //StateManager.PushState();
            }
            if (sender == options)
            {
                //StateManager.PushState()
            }
            if (sender == exitGame)
            {
                GameRef.Exit();
            }
        }

        #endregion
    }
}
